package com.PrayTime;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import muslim.toolkit.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.PrayTime.HijriCalendar.HijriCalendar;
import com.activity.MyApplicationMainActivity;
import com.commun.ArabicUtilities;
import com.commun.Param;

public class PrayTimeLayout extends LinearLayout {
	LinearLayout.LayoutParams textlayoutparams = new LinearLayout.LayoutParams(
			70, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	LinearLayout.LayoutParams layoutparamsWRAP_CONTENT = new LinearLayout.LayoutParams(
			android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
			android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
			android.view.ViewGroup.LayoutParams.FILL_PARENT,
			android.view.ViewGroup.LayoutParams.FILL_PARENT);

	public PrayTimeLayout(Context context) {
		super(context);
		// this.setId(R.id.pray_time_layout);
		ArrayList<String> prayerTimes = getPrayTimeMethodeCal();

		this.setOrientation(LinearLayout.VERTICAL);

		this.setLayoutParams(layoutparams);
		this.setGravity(Gravity.CENTER);
		this.addView(intialiseHijriDateLayout(context));
		this.addView(intialisefakeLayoutToSepareDateOfPrayTime(context));
		
		
		LinearLayout monLinearLayout = new LinearLayout(context);
		monLinearLayout.setLayoutParams(layoutparamsWRAP_CONTENT);
		monLinearLayout.setGravity(Gravity.CENTER);
		monLinearLayout.setOrientation(LinearLayout.VERTICAL);
		monLinearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.pray_time_bg));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(0),
				R.string.salat_fajr, R.string.fajr));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(1),
				R.string.salat_chourouk, R.string.shourouq));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(2),
				R.string.salat_dhohr, R.string.dhohr));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(3),
				R.string.salat_asr, R.string.asr));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(4),
				R.string.salat_maghreb, R.string.maghreb));
		monLinearLayout.addView(linearLayoutTimeOfPray(context, prayerTimes.get(5),
				R.string.salat_isha, R.string.isha));

		this.addView(monLinearLayout);
	}

	public static void Renitialisation(Context context, int meth_cal) {

		RelativeLayout ongletSalatTimeLinearLayout = (RelativeLayout) ((Activity) context)
				.findViewById(R.id.OngletSalatTime);
		LinearLayout layout = (LinearLayout) ongletSalatTimeLinearLayout
				.getChildAt(1);

		layout.setVisibility(View.INVISIBLE);
		ongletSalatTimeLinearLayout.removeViewAt(1);

		ongletSalatTimeLinearLayout.addView(new PrayTimeLayout(context));

	}

	private HijriCalendar getHihriCalendarDates() {

		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);

		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)
				+ Param.CORRECTION_CALENDAR_POUR_HIJRI_MONTH;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return new HijriCalendar(year, month, day);
	}

	private LinearLayout intialiseHijriDateLayout(Context context) {

		// hijri title

		HijriCalendar hijriCalendar = getHihriCalendarDates();

		LinearLayout.LayoutParams hijrilayoutparams = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		LinearLayout monLinearLayout0 = new LinearLayout(context);
		monLinearLayout0.setOrientation(LinearLayout.HORIZONTAL);
		monLinearLayout0.setLayoutParams(hijrilayoutparams);
		monLinearLayout0.setGravity(Gravity.CENTER);
		//monLinearLayout0.setBackgroundDrawable(getResources().getDrawable(R.drawable.text_box));
		String locale = context.getResources().getConfiguration().locale
				.getDisplayName();

		String arabe = context.getString(R.string.arabe);
		String text = "";

		if (locale.equals(arabe))
			text = hijriCalendar.getHijriYear() + "";
		else
			text = hijriCalendar.getHijriDay() + "";
		int couleur = Color.rgb(121, 61, 0);
		int size = 18;
		TextView monTextView0 = new TextView(context);
		monTextView0.setLayoutParams(textlayoutparams);
		monTextView0.setText(text);
		monTextView0.setTextColor(couleur);
		monTextView0.setGravity(Gravity.CENTER);
		monTextView0.setTextSize(size);
		monLinearLayout0.addView(monTextView0);

		TextView monTextViewHijri = new TextView(context);
		monTextViewHijri.setLayoutParams(layoutparamsWRAP_CONTENT);
		monTextViewHijri = ArabicUtilities.getArabicEnabledTextView(context,
				monTextViewHijri);
		monTextViewHijri.setText(hijriCalendar.getHijriMonthArabe().get(
				hijriCalendar.getHijriMonthName()));

		monTextViewHijri.setTextColor(couleur);
		monTextViewHijri.setTextSize(size);
		monTextViewHijri.setGravity(Gravity.CENTER_HORIZONTAL);
		monLinearLayout0.addView(monTextViewHijri);

		if (locale.equals(arabe))
			text = hijriCalendar.getHijriDay() + "";
		else
			text = hijriCalendar.getHijriYear() + "";

		TextView monTextView00 = new TextView(context);
		monTextView00.setLayoutParams(textlayoutparams);
		monTextView00.setText(text);
		monTextView00.setTextColor(couleur);
		monLinearLayout0.addView(monTextView00);
		monTextView00.setTextSize(size);
		monTextView00.setGravity(Gravity.CENTER);

		return monLinearLayout0;
	}

	private LinearLayout intialisefakeLayoutToSepareDateOfPrayTime(
			Context context) {
		int theEmptySpace = 6;
		LinearLayout.LayoutParams fakelayoutparams = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, theEmptySpace);
		LinearLayout monLinearLayoutFake = new LinearLayout(context);
		monLinearLayoutFake.setOrientation(LinearLayout.HORIZONTAL);
		monLinearLayoutFake.setLayoutParams(fakelayoutparams);
		monLinearLayoutFake.setGravity(Gravity.CENTER);
		return monLinearLayoutFake;
	}

	private ArrayList<String> getPrayTimeMethodeCal() {
		ArrayList<String> prayerTimes = PrayTime.getInstance()
				.GetPrayTimeStringsCustom(
						PreferenceManager.getDefaultSharedPreferences(
								MyApplicationMainActivity.getmContext())
								.getInt(Param.PREF_PRAY_TIME_CAL_METHODE, 0));// Egypt
		return prayerTimes;
	}

	private LinearLayout linearLayoutTimeOfPray(Context context, String pray,
			int prayInArabe, int prayInFr) {
		LinearLayout monLinearLayout = new LinearLayout(context);
		monLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
		monLinearLayout.setLayoutParams(layoutparamsWRAP_CONTENT);
		monLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

		TextView monTextViewPrayInFr = new TextView(context);
		monTextViewPrayInFr.setLayoutParams(textlayoutparams);
		monTextViewPrayInFr.setText(prayInFr);
		monTextViewPrayInFr.setGravity(Gravity.LEFT);
		monTextViewPrayInFr.setTextColor(Color.WHITE);
		monLinearLayout.addView(monTextViewPrayInFr);

		TextView time = new TextView(context);
		time.setLayoutParams(textlayoutparams);
		time.setText(pray);
		time.setGravity(Gravity.CENTER);
		time.setTextColor(Color.WHITE);
		monLinearLayout.addView(time);

		TextView monTextViewPrayInAr = new TextView(context);
		monTextViewPrayInAr.setLayoutParams(textlayoutparams);
		monTextViewPrayInAr.setText(prayInArabe);
		monTextViewPrayInAr.setGravity(Gravity.RIGHT);
		monTextViewPrayInAr.setTextColor(Color.WHITE);
		monLinearLayout.addView(monTextViewPrayInAr);

		// ImageView imageView1 = new ImageView(context);
		// imageView1.setLayoutParams(layoutparams);
		// imageView1.setImageResource(prayInArabe);
		// monLinearLayout.addView(imageView1);
		return monLinearLayout;
	}

}